﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using System;
using System.Runtime.Serialization;

namespace FI.Core
{
    /// <summary>
    /// The base class of all concrete stateful entry.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Stateful"), DataContract(Namespace = Constants.Namespace)]
    [Serializable]
    public abstract class StatefulEntry
    {
        #region Properties
        /// <summary>
        /// Gets the state.
        /// </summary>
        /// <value>
        /// The state.
        /// </value>
        [DataMember]
        public EntryState State { get; internal set; }

        /// <summary>
        /// Gets the parent.
        /// </summary>
        /// <value>
        /// The parent.
        /// </value>
        public object Parent { get; internal set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="StatefulEntry"/> class.
        /// </summary>
        protected StatefulEntry()
        {
            this.State = EntryState.Detached;
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Called when property value is specified.
        /// </summary>
        /// <param name="originalValue">The original value.</param>
        /// <param name="value">The property value to be specified.</param>
        protected virtual void OnPropertyChanged(object originalValue, object value)
        {
            if (this.State != EntryState.Unchanged)
            {
                return;
            }

            if (originalValue == null)
            {
                if (value != null)
                {
                    this.State = EntryState.Modified;
                }
                return;
            }
            if (originalValue.Equals(value))
            {
                return;
            }
            this.State = EntryState.Modified;
        }
        #endregion
    }
}